/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Petani;

import java.util.ArrayList;
import java.util.List;
import javax.swing.table.AbstractTableModel;

/**
 *
 * @author gatz
 */
public class PetaniTabelModel extends AbstractTableModel {

    private List<Petani> petanis = new ArrayList<Petani>();

    public PetaniTabelModel(List<Petani> petani2) {
        this.petanis = petani2;
    }

    public void deletePetani(int row) {
        petanis.remove(row);
        fireTableRowsDeleted(row, row);
    }

    public void updatePetani(int row, Petani petani) {
        petanis.set(row, petani);
        fireTableRowsUpdated(row, row);
    }

    public void addPetani(Petani petani) {
        petanis.add(petani);
        fireTableRowsInserted(getRowCount() - 1, getColumnCount() - 1);
    }

    public int getRowCount() {
        return petanis.size();
    }

    public int getColumnCount() {
        return 3;
    }

    public Object getValueAt(int rowIndex, int columnIndex) {
        Petani b = petanis.get(rowIndex);
        switch (columnIndex) {
            case 0:
                return b.getIdPetani();
            case 1:
                return b.getNamaPetani();
            case 2:
                return b.getDaerah().getIdDaerah() + " " + b.getDaerah().getNamaDaerah();               
            default:
                return "";
        }
    }

    @Override
    public String getColumnName(int column) {
        switch (column) {
            case 0:
                return "ID PETANI";
            case 1:
                return "NAMA PETANI";
            case 2:
                return "DAERAH";
            default:
                return "";
        }
    }
}
